import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      component: () => import('./views/userInfo'),
      meta: {
        auth: true
      }
    }, {
      path: '/login',
      component: () => import('./views/login')
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(route => route.meta.auth)) {
    if (sessionStorage.getItem('user')){
      next()
    }else{
      next({
        path: '/login',
        query: {
          returnURL: to.path
        }
      })
    }
    
  } else {
    next()
  }
})

export default router